package com.example.com.gemptc.example;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MyListAdapter extends BaseExpandableListAdapter {

	private Context context;
	private ArrayList<HeaderInfo> deptList;

	public MyListAdapter(Context context, ArrayList<HeaderInfo> deptList) {
		this.context = context;
		this.deptList = deptList;
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		ArrayList<DetailInfo> productList = deptList.get(groupPosition)
				.getProductList();
		return productList.get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View view, ViewGroup parent) {

		DetailInfo detailInfo = (DetailInfo) getChild(groupPosition,
				childPosition);
		if (view == null) {
			LayoutInflater infalInflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = infalInflater.inflate(R.layout.child_row, null);
		}

		ImageView qqimg = (ImageView) view.findViewById(R.id.qqimg);
		qqimg.setImageResource(R.drawable.qq);
		TextView childItem = (TextView) view.findViewById(R.id.childItem);
		childItem.setText(detailInfo.getName().trim());

		return view;
	}

	@Override
	public int getChildrenCount(int groupPosition) {

		ArrayList<DetailInfo> productList = deptList.get(groupPosition)
				.getProductList();
		return productList.size();

	}

	@Override
	public Object getGroup(int groupPosition) {
		return deptList.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return deptList.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, View view,
			ViewGroup parent) {

		HeaderInfo headerInfo = (HeaderInfo) getGroup(groupPosition);
		if (view == null) {
			LayoutInflater inf = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = inf.inflate(R.layout.group_heading, null);
		}
		
		ImageView indicator=(ImageView)view.findViewById(R.id.iv_indicator);
		if (isExpanded) {
			indicator.setImageResource(R.drawable.indicator_expanded);
		}else{
			indicator.setImageResource(R.drawable.indicator_unexpanded);
		}
		
		TextView heading = (TextView) view.findViewById(R.id.heading);
		heading.setText(headerInfo.getName().trim());

		return view;
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

}